import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;
import javax.sql.DataSource;
import java.sql.*;
import java.util.Scanner;
public class Test {
    public static void main(String[] args) throws SQLException {
        //创建dataSource
        DataSource dataSource = new MysqlDataSource();
        ((MysqlDataSource) dataSource).setURL("jdbc:mysql://127.0.0.1:3306/jdbc?characterEncoding=utf8&useSSL=false");
        ((MysqlDataSource) dataSource).setUser("root");
        ((MysqlDataSource) dataSource).setPassword("cxh0805");
        //连接数据库
        Connection connection = dataSource.getConnection();
        //通过控制台。来进行插入
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入学号");
        int id = scanner.nextInt();
        System.out.println("请输入姓名");
        String name = scanner.next();
        System.out.println("请输入数学成绩");
        double math=scanner.nextDouble();
        System.out.println("请输入语文成绩");
        double chinese=scanner.nextDouble();
        System.out.println("请输入英语成绩");
        double english=scanner.nextDouble();
        String sql = "insert into mark values(id,name,math,chinese,english)";
        PreparedStatement Statement = connection.prepareStatement(sql);
        Statement.setInt(1, id);
        Statement.setString(2, name);
        Statement.setDouble(3,math);
       Statement.setDouble(4,chinese);
        Statement.setDouble(5,english);
        int row1 = Statement.executeUpdate();
        Statement.close();
        connection.close();
    }
    public static void main1(String[] args) throws SQLException {
        DataSource dataSource = new MysqlDataSource();
        ((MysqlDataSource)dataSource).setURL("jdbc:mysql://127.0.0.1:3306/tey?" +
                "characterEncoding=utf8&useSSL=false");
        ((MysqlDataSource)dataSource).setUser("root");//数据库的用户名，不改动就是root
        ((MysqlDataSource)dataSource).setPassword("xxxxx");//数据库的密码
        // 2. 和数据库建立连接
        Connection connection = dataSource.getConnection();
        System.out.println("连接成功");
    }
}
